﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OliveAirLeakTest
{
    public partial class QueryFrm : Form
    {
        public QueryFrm()
        {
            InitializeComponent();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if(dgvData.DataSource ==null || dgvData.Rows.Count ==0)
            {
                MessageBox.Show("数据为空，不能导出", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    ExcelHelper.ToExcel((DataTable)dgvData.DataSource);
                    MessageBox.Show("数据导出成功");
                }
                catch(Exception ex)
                {
                    MessageBox.Show($"数据导出 时出错错误：{ex.Message}");
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void QueryFrm_Load(object sender, EventArgs e)
        {
            cmbProductName.Items.Clear();
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            List<Product> products =  sqlSugarHelper.QueryAll<Product>();
            foreach (Product product in products)
            {
                cmbProductName.Items.Add(product.Name);
            }

            dtpStartTime.Value = DateTime.Now;
            dtpStartTime.Format = DateTimePickerFormat.Custom;
            dtpStartTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            //dtpStartTime.ShowUpDown= true;
            dtpEndTime.Value = DateTime.Now;
            dtpEndTime.Format = DateTimePickerFormat.Custom;
            dtpEndTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            Expression<Func<Recode, bool>> whereProductName = it => it.ProductName == cmbProductName.Text;
            Expression<Func<Recode,bool>> whereStartTime = p =>p.TestTime >= dtpStartTime.Value;
            Expression<Func<Recode, bool>> whereEndTime = p =>p.TestTime <= dtpEndTime.Value;
            Expression<Func<Recode, bool>> whereCode = p => p.Code.Contains(rtbCode.Text);
            //var dt = sqlSugarHelper.QueryDataToTable<Recode>(cbProductName.Checked, whereProductName,cbStartTime.Checked,whereStartTime,cbEndTime.Checked,whereEndTime,cbCode.Checked,whereCode);


            Dictionary<Expression<Func<Recode, bool>>,bool> dict = new Dictionary<Expression<Func<Recode, bool>>,bool>();
            dict.Add( whereProductName, cbProductName.Checked);
            dict.Add(whereStartTime,cbStartTime.Checked );
            dict.Add(whereEndTime,cbEndTime.Checked);
            dict.Add(whereCode,cbCode.Checked);

            var dt = sqlSugarHelper.QueryDataToTable(dict);
           
            dgvData.DataSource = dt;
        }

        
    }
}
